package com.sina.shop.controller.collect;

import com.sina.shop.core.exception.server.UpdateException;
import com.sina.shop.domain.collect.CollectGoods;
import com.sina.shop.service.collect.CollectGoodsService;
import com.sina.shop.web.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.ModelAndView;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * Created with IntelliJ IDEA.
 * Description:
 * 段浩杰   2018-04-12-17:45
 */
@Controller
@RequestMapping("/shop-web/collectGoods")
public class CollectGoodsController extends BaseController {

    @Autowired
    private CollectGoodsService collectGoodsService;

    @RequestMapping(value = "/getCollectGoodsByAdminId", method = RequestMethod.GET)
    public ModelAndView getCollectGoodsByAdminId(@RequestParam("adminId") Long adminId) {
        LOG.info("adminId:{}", adminId);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        List<CollectGoods> collectGoodsList = collectGoodsService.getCollectGoodsByAdminId(adminId);

        data.put("collectGoodsList", collectGoodsList);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/getCollectGoodsByAidAndBid", method = RequestMethod.GET)
    public ModelAndView getCollectGoodsByAidAndBid(@RequestParam("adminId") Long adminId, @RequestParam("goodsId") Long goodsId) {
        LOG.info("adminId:{},goodsId:{}", adminId, goodsId);
        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> data = new HashMap<>(4);
        Map<String, Object> result = new HashMap<>(6);
        CollectGoods collectGoods = collectGoodsService.getCollectGoodsByAidAndBid(adminId, goodsId);

        data.put("collectGoods", collectGoods);
        result.put(CODE, OK);
        result.put(DATA, data);
        mv.addObject(RESULT, result);

        return mv;
    }

    @RequestMapping(value = "/saveCollectGoods", method = RequestMethod.POST)
    public ModelAndView saveCollectGoods(CollectGoods collectGoods) {

        LOG.info("collectGoods:{}", collectGoods);
        collectGoods.setCreateTime(LocalDateTime.now());

        ModelAndView mv = new ModelAndView("commonView");
        Map<String, Object> result = new HashMap<>(4);
        try {
            collectGoodsService.saveCollectGoods(collectGoods);
            result.put(CODE, OK);
            result.put(MSG, "新增收藏商品成功");
        } catch (UpdateException e) {
            LOG.info("新增收藏商品出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }

    @RequestMapping(value = "/deleteCollectGoods", method = RequestMethod.POST)
    public ModelAndView deleteCollectGoods(@RequestParam("id") Long id) {
        LOG.info("id:{}", id);
        Map<String, Object> result = new HashMap<>(6);
        ModelAndView mv = new ModelAndView("commonView");

        try {
            collectGoodsService.deleteCollectGoods(id);
            result.put(CODE, OK);
            result.put(MSG, "删除收藏商品成功");
        } catch (UpdateException e) {
            LOG.info("删除收藏商品出现异常:{}", e.getMessage());
            result.put(CODE, e.getResultCode());
            result.put(MSG, e.getMessage());
        }

        mv.addObject(RESULT, result);
        return mv;
    }
}
